$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'basics/blocation/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '位置名称', name: 'name', index: 'name', width: 80 ,align:"center" },
			{ label: '方位', name: 'position', index: 'position', width: 80,hidden:true },
			{ label: '方位', name: 'positionName', index: 'position_name', width: 80 ,align:"center" },
			{ label: '库房id', name: 'warehouseId', index: 'warehouse_id', width: 80,hidden:true },
			{ label: '库房id', name: 'warehouseName', index: 'warehouse_name', width: 80 ,align:"center" ,hidden:true},
			{ label: '备注', name: 'remark', index: 'remark', width: 80  ,hidden:true},
			{ label: '系统id', name: 'systemId', index: 'system_id', width: 80 ,hidden:true},
			{ label: '创建人', name: 'createId', index: 'create_id', width: 80,hidden:true },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80,hidden:true },
			{ label: '跟新人', name: 'updateId', index: 'update_id', width: 80 ,hidden:true},
			{ label: '跟新时间', name: 'updateTime', index: 'update_time', width: 80,align:"center" },
			{ label: '是否删除 0 否 1是', name: 'isDel', index: 'is_del', width: 80,hidden:true },
			{ label: '是否禁用', name: 'isUsed', index: 'is_used', width: 80 ,align:"center",formatter: function(value, options, row){
                    return value === 1 ?
                        '<span class="label label-success" value="1">启用</span>' :
                        '<span class="label label-danger" value="0">禁用</span>';
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.pageNum",
            total: "page.pages",
            records: "page.total"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        postData:{
            name: vm.search.name,
            warehouseId:localStorage.getItem("house"),
            systemId:localStorage.getItem("sysManageId")
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bLocation: {},
        isShow:false,//是否只读
        bLocationPositionList:[],//数组
        ruleValidate:{//验证
            name: [{ required: true,trigger: 'blur',message:'请输入位置名称！'}],
            position: [{required: true,type:'number', trigger: 'change',message:'请选择方位！'}],
        },
		search:{
			name:''
		},
		manage:[],
        isUsed:true,//是否有效 默认有效
	},
	mounted:function(){
    	this.initList();
    	this.initSysManger();
	},
	methods: {
		initList:function(){
            $.get(baseURL + "basics/blocation/getPostionList/", function(r){
                vm.bLocationPositionList = r.list;
            });
		},
		initSysManger:function(){
            $.get(baseURL + "sys/manage/all/", function(r){
                vm.manage = r.list;
                vm.bLocation.systemId=r.list[0];
            });
		},
		query: function () {
			vm.reload();
		},
        reset:function(event) {
            vm.search = {
               name:'',
            }
            vm.reload();
        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bLocation = {};
			vm.bLocation.position=vm.bLocationPositionList.length>0?vm.bLocationPositionList[0].id:null;
			vm.bLocation.warehouseId=localStorage.getItem('house');
			vm.bLocation.systemId=localStorage.getItem('systemId');
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.bLocation.id == null ? "basics/blocation/save" : "basics/blocation/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bLocation),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "basics/blocation/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "basics/blocation/info/"+id, function(r){
                vm.bLocation = r.bLocation;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{
                    name: vm.search.name,
                    wareHouseId:localStorage.getItem("house"),
                    systemId:localStorage.getItem("sysManageId")
                }
            }).trigger("reloadGrid");
		},
        selectChange:function(selVal){
            this.$forceUpdate()
        },
        changeUsed:function(){
            vm.bLocation.isUsed=vm.isUsed?1:0;
        }
	}
});